package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyEsService strategyEsService;


    @RequestMapping("/detail")
    public String detail(Model model, Long id) {
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail", detail);

        //阅读数
        strategyStatisVOService.increaseViewNum(id, 1);
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(id);
        model.addAttribute("vo", vo);

        //阅读攻略排行加分
        strategyStatisVOService.addScoreHot(id, 1);
        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        //dest
        model.addAttribute("dest", destination);

        //tags
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags", tags);

        return "strategy/list";
    }

    //评论操作
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo user) {
        comment.setCity(user.getCity());
        comment.setHeadUrl(user.getHeadImgUrl());
        comment.setLevel(user.getLevel());
        comment.setUserId(user.getId());
        comment.setUsername(user.getNickname());
        //保存评论
        strategyCommentService.save(comment);
        //保存之后评论数加 1
        strategyStatisVOService.increaseReplyNum(comment.getDetailId(), 1);
        //共享vo对象
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(comment.getDetailId());
        return AjaxResult.SUCCESS.addData(vo.getReplynum());//评论数据
    }

    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo) {
        //page : spring-data里面的page
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page", page);
        return "strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, @UserParam UserInfo user) {
        strategyCommentService.commentThumbUp(toid, user.getId());
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo user) {
        //登录验证
        if (user == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102); //未登录操作
            return result;
        }
        // 收藏, 取消收藏
        boolean ret = strategyStatisVOService.favor(sid, user.getId());
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(sid);

        //收藏: 国内外排行的加分
        strategyStatisVOService.addScoreAbroadAndChina(sid, ret ? 1 : - 1);
        return new AjaxResult(ret, "").addData(vo);
    }

    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo user) {
        //登录验证
        if (user == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102); //未登录操作
            return result;
        }
        //顶, 今天已经顶过了
        boolean ret = strategyStatisVOService.thumbup(sid, user.getId());
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(sid);

        //点赞国内外排行
        if (ret) {
            strategyStatisVOService.addScoreAbroadAndChina(sid, 1);
        }
        return new AjaxResult(ret, "").addData(vo);
    }

    @RequestMapping("")
    public String index(Model model) {
        //攻略推荐
        List<StrategyCommend> commends = strategyCommendService.getTop5();
        model.addAttribute("commends", commends);

        //查询热门前10
        List<StrategyStatisVO> hotCds = strategyStatisVOService.queryHotSortTop10();
        model.addAttribute("hotCds", hotCds);

        //abroadCds
        List<StrategyStatisVO> abroadCds = strategyStatisVOService.queryAbroadSortTop10();
        model.addAttribute("abroadCds", abroadCds);
        //unabroadCds
        List<StrategyStatisVO> unabroadCds = strategyStatisVOService.queryChinaSortTop10();
        model.addAttribute("unabroadCds", unabroadCds);

        //主题推荐
        //themeCds
        List<Map<String, Object>> data = strategyEsService.queryThemeCommend();
        model.addAttribute("themeCds", data);

        //国内
        //chinas
        List<StatisVO> chinas = strategyEsService.queryConditionChina();
        model.addAttribute("chinas", chinas);
        //国外
        //abroads
        List<StatisVO> abroads = strategyEsService.queryConditionAboard();
        model.addAttribute("abroads", abroads);
        //主题
        //themes
        List<StatisVO> themes = strategyEsService.queryConditionThemes();
        model.addAttribute("themes", themes);
        return "strategy/index";
    }
    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo")StrategySearchQuery qo) {
        Page page = strategyEsService.query(qo);
        model.addAttribute("page", page);
        return "strategy/searchPageTpl";
    }
    //********************************************************************
    //分享
    @RequestMapping("/intoShare")
    @ResponseBody
    public Object intoShare(@UserParam UserInfo userInfo, Long detailId){
        if(userInfo!=null){
            //不为空则进行分享数加一操作
            try {
                strategyStatisVOService.UpdateSharenum(detailId);
            }catch (Exception e){
                e.printStackTrace();
                return new AjaxResult(false,"分享操作失败");
            }
        }else {
            //提示未登录
            return new AjaxResult(false,"铁汁,登录好吗");
        }
        return AjaxResult.SUCCESS.addData(strategyStatisVOService.getStrategyStatisVO(detailId));
    }
    //********************************************************************
}
